package com.yxq.mdt.app.modules.login.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by L on 2019-10-18.
 */
public class LoginModel implements IBaseModel {

    public void login(String account, String password, ApiModel.CommonCallback<UserBean> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .login(account, password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            UserBean userBean = JSON.parseObject(respBase.getData(), UserBean.class);
                            callback.onSuccess(userBean);
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void loginCode(String account, String code, ApiModel.CommonCallback<UserBean> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .loginCode(account, code)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            UserBean userBean = JSON.parseObject(respBase.getData(), UserBean.class);
                            callback.onSuccess(userBean);
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void loginAPP3(String openid, String nickname, String avatar_url, String platform, ApiModel.CommonCallback<UserBean> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .loginApp3(openid, nickname, avatar_url, platform)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            UserBean userBean = JSON.parseObject(respBase.getData(), UserBean.class);
                            callback.onSuccess(userBean);
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void verifyCode(String account, int sms_sce, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .verifyCode(account, sms_sce)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public static void bindVerifyCode(String account, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .bindVerifyCode(account, Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public static void bindWechat(String openId,String nickname,String avatar_url, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .bindWechat(openId, Constant.getToken(),nickname,avatar_url)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public static void updateBindPhone(String account, String sms_code, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .updateBindPhone(account, sms_code, 2, Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void register(String account, String password, String password2, int platform, String sms_code, String invite, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .register(account, password, password2, platform, sms_code, invite)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void forget(String account, String password, String password2, int platform, String sms_code, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .forget(account, password, password2, platform, sms_code)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void bindPhone(String account, String password, String token, String sms_code, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(LoginApi.class)
                .bindPhone(account, password, token, sms_code)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {

    }
}
